Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom document layouts with y-prosemirror #13

Merged
merged 2 commits into from
Mar 15, 2020
Merged

Conversation

BrianHung
Copy link
Contributor

@BrianHung BrianHung commented Mar 14, 2020

The following commit allows y-prosemirror to work with documents with forced, custom layouts. An example of this is a document with a required title, with the schema defined as content: "title block+".

Previously, it was assumed that an empty ProseMirror document is a single paragraph node, with content size of 2. As a result, when ProseMirror initializes an empty document with a custom layout (with content size > 2), the sync-plugin erroneously triggers a call to binding._prosemirrorChanged even though the initial content has not changed.

Because of this, yjs will not be able to initialize a serialized document correctly with Y.applyUpdate(ydoc, ystate): instead of merging the nodes from ystate and the empty nodes created by the ProseMirror layout, it will duplicate the nodes. For example, instead of ["title", "paragraph"], yjs will initialize the document to ["title", "paragraph", "title", "paragraph"].

To calculate the size of an empty ProseMirror document with any custom layout, we use view.state.doc to obtain the node describing the schema (layout) and call createAndFill().content.size (ProseMirror documentation here). We then use this constant instead of 2 in the comparison for changedInitialContent.

The following commit allows y-prosemirror to work with documents with forced, custom layouts. An example of this is a document with a required title, with the schema defined as `content: "title block+"`.

Previously, it was assumed that an empty ProseMirror document is a single paragraph node, with content size of 2. As a result, when ProseMirror initializes an empty document with a custom layout (with content size > 2), the sync-plugin erroneously triggers a call to `binding._prosemirrorChanged` even though the initial content has not changed.

To calculate the size of an empty ProseMirror document with *any* custom layout, we use `view.state.doc` to obtain the node describing the schema (layout) and call `createAndFill().content.size` (ProseMirror documentation [here](https://prosemirror.net/docs/ref/#model.NodeType.createAndFill)). We then use this constant instead of 2 in the comparison for `changedInitialContent`.
@BrianHung BrianHung changed the title forced, custom layouts with y-prosemirror custom document layouts with y-prosemirror Mar 14, 2020
Forgot to include `.type` after `doc`.
@dmonad
Copy link
Member

dmonad commented Mar 15, 2020

This all makes sense. Thanks for the PR! I'm going to merge and release in a bit.

@dmonad dmonad merged commit 112533e into yjs:master Mar 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants